


import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
import javax.jms.QueueConnectionFactory;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.sun.messaging.jmq.jmsserver.core.Session;


public class Main {
	
	public void getMessages()
	{
		Context jndiContext = null;
		ConnectionFactory connectionFactory = null;
		Queue queue = null;
		try{
			jndiContext = new InitialContext();
		}
		catch(NamingException e){
			System.out.println("nu putem crea contextul JNDI API " + e.getMessage());
		}
		try {
			connectionFactory = (QueueConnectionFactory) jndiContext.lookup("jms/Firstjms");
			queue = (Queue) jndiContext.lookup("jms/Firstjmsqueue");
		}
		catch(NamingException e)
		{
			System.out.println("cautare esuata in JNDI API: " +e.getMessage());
		}
		
		Connection connection;
		MessageConsumer messageConsumer;
		TextMessage textMessage;
		boolean goodByeRecieved = false;
		
		
			try {
				connection = connectionFactory.createConnection();
			
			javax.jms.Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			messageConsumer = session.createConsumer(queue);
			connection.start();
			while(!goodByeRecieved) {
				System.out.println("asteptam mesaje...");
				textMessage =(TextMessage) messageConsumer.receive();
					if(textMessage != null)
					{
						System.out.println("am receptionat urmatorul mesaj: ");
						System.out.println(textMessage.getText());
						System.out.println();
						
					}
			
			}
			messageConsumer.close();
			session.close();
			connection.close();
			} catch (JMSException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
	}
	
	public static void main(String[] args) {
		
		new Main().getMessages();
		
	}

	/* (non-Java-doc)
	 * @see java.lang.Object#Object()
	 */
	public Main() {
		super();
	}

}